#ifndef BUYCATALOG_H
#define BUYCATALOG_H

#include "ui_buycatalog.h"

#include <QtGui/QDialog>
#include <iapclient.h>

class ProductInfo;
class QProgressDialog;

class BuyCatalog : public QDialog
{
    Q_OBJECT

public:
    explicit BuyCatalog(QWidget *parent = NULL);
    virtual ~BuyCatalog();

    inline void setDefaultProduct(QString& product){
        defaultProduct = product;
    }

private:
    void requestNextProduct();
    bool isProductActivated(QString product);

private slots:

    /*
     * In-Application Purchase specific slots
     *
     * Slots matching the signals of the In-Application Purchase API, allowing
     * the application to receive callbacks
     */
    void productDataReceived( int requestId, QString status,
                              IAPClient::ProductDataHash productData );
    void purchaseCompleted( int requestId, QString status, QString purchaseTicket );
    void purchaseFlowFinished( int requestId );
    void userAndDeviceDataReceived( int requestId, QString status,
                                    IAPClient::UserAndDeviceDataHash userdata );
    void restorableProductsReceived( int requestId, QString status,
                                     IAPClient::ProductDataList items );
    void restorationFlowFinished( int requestId );
    void restorationCompleted( int requestId, QString status, QString purchaseTicket );

private slots:

    /*
     * Slot to handle user's tap on the Buy button on a catalog's product item.
     */
    void buyProduct();

    /*
     * Slot to handle user's tap on a catalog's product item, as a way of
     * accessing more information about it.
     *
     * QListWidgetItem* item - the list item on which the tap occured
     */
    void moreProductInfo(QListWidgetItem* item);

    /*
     * infoDialogClosed will be closed when the opened infoDialog gets closed.
     *
     * int result - the reason (positive or negative choice) for which the dialog was closed
     */
    void infoDialogClosed(int result);

    /*
     * Slot to handle user's tap on the Restore button on a catalog's product item.
     */
    void restoreProducts();

private:
    /*
     * UI class.
     */
    Ui::BuyCatalogClass ui;

    QString defaultProduct;
    /*
     * List of product items as read from app's config file.
     */
    QStringList products;

    /*
     * Index in *products* list indicating which is the last product for which
     * info was requested from Ovi
     */
    int productsRequested;

    /*
     * Holds the requestId returned by the current In-Application Purchase call
     */
    int current_requestId;

    QString current_productId;

    /*
     * In-Application Purchase API
     */
    IAPClient *iap_client;

    /*
     * List of availble products as retrieved from Ovi
     */
    QList<IAPClient::ProductDataHash> available_products;

    /*
     * Dialog showing the full IAPClient::ProductData product description
     */
    ProductInfo* infoDialog;

    /*
     * The index of the product for which more info is displayed.
     */
    int current_product_index;

    IAPClient::ProductDataList restorableProductItems;
    QProgressDialog* busyIndicator;
};

#endif // BUYCATALOG_H
